package MyAdmin.Controller;

import MyFramePro.Constants.SystemConstants;
import MyFramePro.Domain.Entity.Category;
import MyFramePro.Domain.ResponseResult;
import MyFramePro.Domain.Vo.CategoryVo;
import MyFramePro.Domain.Vo.ExcelCategoryVo;
import MyFramePro.Enums.AppHttpCodeEnum;
import MyFramePro.Service.Impl.CategoryServiceImpl;
import MyFramePro.Utils.BeanCopyUtils;
import MyFramePro.Utils.WebUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping(value = "/content/category")
public class CateGoryController {

    @Resource
    private CategoryServiceImpl categoryService;

    /**
     * 查询分类集合的功能
     */
//    @PreAuthorize("@myPer.hasAuthority('content:category:listAllCategory')") //这里进行传入权限信息
    @GetMapping(value = "/listAllCategory")
    public ResponseResult listAllCategory(){

        List<CategoryVo>  categoryVoList=categoryService.listAllCategory();

        return  ResponseResult.okResult(categoryVoList);
    }


    /**
     * 导出所有分类到 Excel 中
     */
    @PreAuthorize("@myPer.hasAuthority('content:category:export')") //这里进行传入权限信息
    @GetMapping(value = "/export")
    public void saveExcel(HttpServletResponse response){

        try {
            //1.设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);

            //2.获取需要导出的数据
            List<Category> categories = categoryService.list();
            //2.1由于转换时所需的字段，这里使用 Vo 进行封装
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.myCopyBeanList(categories, ExcelCategoryVo.class);

            //3.将数据导入到 Excle 中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class)
                    .autoCloseStream(Boolean.FALSE).sheet("分类导出")   //这里是 Excel 中工作簿的名称
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
//            e.printStackTrace();
            //4.若出现异常则给出提示信息
            ResponseResult errorResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);

            WebUtils.renderString(response, JSON.toJSONString(errorResult));
        }

    }


    /**
     * 这里是分页查询分类列表的功能
     */
    @GetMapping(value = "/list")
    public ResponseResult categoryPageList(Integer pageNum,Integer pageSize,String name,String status){

        return categoryService.getCagegoryPageList(pageNum,pageSize,name,status);
    }


    /**
     * 这里是新增分类的功能
     */
    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryVo categoryVo){

        return categoryService.addCategory(categoryVo);
    }


    /**
     * 修改分类————查询分类信息功能
     */
    @GetMapping("{id}")
    public ResponseResult selectCatrgoryInfo(@PathVariable("id") Long id){

        return categoryService.selectCatrgoryInfo(id);
    }


    /**
     * 修改分类————更新分类功能
     */
    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryVo categoryVo){

        return categoryService.updateCategory(categoryVo);
    }


    /**
     * 这里是删除分类的功能
     */
    @DeleteMapping("{id}")
    public ResponseResult deleteCategory(@PathVariable("id") Long id){

        return categoryService.deleteCategory(id);
    }

}
